// pages/details/details.js
const {
  getdetaildate,
  getsopcarprice,
  getaddsoopcar,
} = require("../../http/api.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    old: "",
    nwprice: "",
    detalist: "",
    name: "",
    wxml: "",
    show: false,
    soopcarsrc: "",
    properties: "",
    str: "",
    xqid: "",
    slidid: "",
    sooprice: "",
    properArr: [],
    sum: 1,
    num: 1,
    soopcarstr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.xqid = options.id
    getdetaildate(options.id).then(res => {
      this.setData({
        background: res.data.data.pics,
        old: res.data.data.basicInfo.minPrice,
        nwprice: res.data.data.basicInfo.originalPrice,
        name: res.data.data.basicInfo.name,
        detalist: res.data.data.basicInfo.characteristic,
        wxml: res.data.data.content,
        soopcarsrc: res.data.data.basicInfo.pic,
      })
      if (res.data.data.properties) {
        this.setData({
          properties: res.data.data.properties
        })
      }
      //添加active字段
      if (res.data.data.properties) {
        res.data.data.properties.forEach(res => {
          res.childsCurGoods.forEach(el => {
            el.active = false
          })
        })
      }
    })
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onClickShow() {
    this.setData({
      show: true
    });
  },
  onClickHide() {
    this.setData({
      show: false
    });
  },
  add() {
    if (this.data.sum > 0) {
      this.setData({
        num: this.data.sum++
      })
    }

  },
  sub() {
    if (this.data.sum > 0) {
      this.setData({
        num: this.data.sum--
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  tosy() {
    wx.navigateBack({
      delta: 1
    })
  },
  childsCurGoodsclick(e) {

    //处理页面的点击操作
    let tabIndex = e.currentTarget.dataset.index;
    this.data.properties[tabIndex[0]].childsCurGoods.forEach((item) => {
      item.active = false;
    })
    this.data.properties[tabIndex[0]].childsCurGoods[tabIndex[1]].active = !this.data.properties[tabIndex[0]].childsCurGoods[tabIndex[1]].active;
    this.setData({
      properties: this.data.properties
    })



    let val = e.target.dataset.id
    let properId = val.split(':')[0];
    let bool = true;
    for (let i = 0; i < this.data.properArr.length; i++) {
      if (this.data.properArr[i].indexOf(properId) !== -1) {
        bool = false
        this.data.properArr.splice(i, 1);
        this.data.properArr.push(val)
      }
    }
    if (bool) { // 如果bool还是true 代表 没有任何重复需要替换的，就直接push
      this.data.properArr.push(val)
      this.data.soopcarstr.push(val)
  
    }
    if (this.data.properArr.length == this.data.properties.length) {
      let str = this.data.properArr.join(',');
      getsopcarprice(this.data.xqid, str).then(res => {
        if (res.data.code == 0) {
          this.setData({
            old: res.data.data.price
          })
        }
      })
    }



  },
  delsoopcar() {
    this.setData({
      show: false
    })
  },
  gosoopcar(){
    wx.reLaunch({
      url: '../../pages/soopcar/soopcar',
    })
  },
  addsoopcar() {

    var arrnew = []
    for (var i = 0; i < this.data.soopcarstr.length; i++) {
      var obj = {}
      obj.optionId = this.data.soopcarstr[i].split(":")[0]
      obj.optionValueId = this.data.soopcarstr[i].split(":")[1]
      arrnew.push(obj)
    }
    getaddsoopcar(this.data.xqid, this.data.num, wx.getStorageSync('token'),JSON.stringify(arrnew)).then(res => {
      console.log(res);
      wx.reLaunch({
        url: '../../pages/soopcar/soopcar',
      })
    })
  }
})